package cn.hsw.component.core.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 获取ApplicationContext对
 * @author 李子木
 * @date 2020-11-05
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext content;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.content = applicationContext;
    }

    /**
     * 获取applicationContext
     */
    public static ApplicationContext getInstance() {
        return content;
    }

    /**
     * 通过class获取Bean.
     */
    public static <T> T getBean(Class<T> clazz){
        return getInstance().getBean(clazz);
    }



}
